<?php
class HangmucsController extends AppController {

	var $name = 'Hangmucs';
	var $uses= array('Ctrinh','Hangmuc');
	var $components=array('Cookie');
	
	/*
	function beforeFilter() {
  		$this->Cookie->name = 'idct';
  		$this->Cookie->time =  3600;  // or '1 hour'
  	//	$this->Cookie->path = '/bakers/preferences/';
  	//	$this->Cookie->domain = 'example.com';
  		$this->Cookie->secure = true;  //i.e. only sent if using secure HTTPS
  		$this->Cookie->key = 'qSI232qs*&sXOw!';
	}*/

	function index($idct) {
		//print_r($idct);
		//$this->Cookie->write('idct',$idct);
		$this->Cookie->write('idct',$idct,true, 3600);
		print_r('dulieu cookie'.$this->Cookie->read('idct'));
		if(empty($idct)){
			$this->Session->setFlash(__('Hạng mục không hợp lệ', true));
			$this->redirect(array('controller'=>'ctrinhs','action' => 'index'));
		}
		
		$this->Hangmuc->recursive = -1;
		$this->set('hangmucs', $this->paginate('Hangmuc',array( 'ctrinh_id'=>$idct)));
		
		$this->Ctrinh->recursive = -1;
		
		$tenct=$this->Ctrinh->find('first',array('conditions'=>array('id'=>$idct),'fields'=>array('tenctrinh')));
		$tenct=$tenct ['Ctrinh'] ['tenctrinh'];
		$this->set(compact('tenct'));
	}


	function add() {
		$idct=$this->Cookie->read('idct');
	//	print_r($this->Cookie->read('idct'));
			
		if (!empty($this->data)) {
			$this->Hangmuc->create();
			$this->data ['Hangmuc'] ['ctrinh_id']=$idct;
			if ($this->Hangmuc->save($this->data)) {
				$this->Session->setFlash(__('The hangmuc has been saved', true));
				$this->redirect(array('action' => 'index',$idct));
			} else {
				$this->Session->setFlash(__('Dữ liệu được lưu, vui lòng thử lại', true));
			}
		}
		$this->set(compact('idct'));
	}

	function edit($id = null) {
		$idct=$this->Cookie->read('idct');
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid hangmuc', true));
			$this->redirect(array('action' => 'index',$idct));
		}
		if (!empty($this->data)) {
			$this->data ['Hangmuc'] ['ctrinh_id']=$idct;
			if ($this->Hangmuc->save($this->data)) {
				$this->Session->setFlash(__('The hangmuc has been saved', true));
				$this->redirect(array('action' => 'index',$idct));
			} else {
				$this->Session->setFlash(__('Dữ liệu được lưu, vui lòng thử lại', true));
			}
		}
		if (empty($this->data)) {
			$this->Hangmuc->recursive=-1;
			$this->data = $this->Hangmuc->read(null, $id);
		}
		$this->set(compact('idct'));
	}

	function delete($id = null) {
		$idct=$this->Cookie->read('idct');
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for hangmuc', true));
			$this->redirect(array('action'=>'index',$idct));
		}
		if ($this->Hangmuc->delete($id)) {
			$this->Session->setFlash(__('Hangmuc deleted', true));
			$this->redirect(array('action'=>'index',$idct));
		}
		$this->Session->setFlash(__('Hangmuc was not deleted', true));
		$this->redirect(array('action' => 'index',$idct));
	}
}
